package com.haojiang.mh.remote.ui.frag

import android.graphics.Color
import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.haojiang.mh.R
import com.haojiang.mh.remote.adapter.BleGroupDialogFragAdapter
import com.haojiang.mh.remote.base.BaseFrag
import com.haojiang.mh.remote.bean.BleRssiDevice
import com.haojiang.mh.remote.callback.IBleItemClickLisenter
import com.haojiang.mh.remote.callback.IGroupInfo
import com.haojiang.mh.remote.enums.EConnect
import com.haojiang.mh.remote.event.BleEvent
import com.haojiang.mh.remote.event.BleStatus
import com.haojiang.mh.remote.ui.dialog.MotorTestDialogFrag
import com.haojiang.mh.remote.utils.BleUtil
import com.haojiang.mh.remote.utils.BleUtil.Companion.ble
import com.haojiang.mh.remote.utils.BleUtil.Companion.devDisconnectAll
import com.haojiang.mh.remote.utils.BleUtil.Companion.devScan
import com.haojiang.mh.remote.utils.BleUtil.Companion.groupData
import com.haojiang.mh.remote.utils.Util
import kotlinx.android.synthetic.main.frag_third.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class FragThird : BaseFrag(), IGroupInfo {

    private val bleGroupDialogFragAdapter by lazy { BleGroupDialogFragAdapter(activity!!, BleUtil.bleRssiDeviceList) }

    /**
     * 点击后回调并连接ble设备
     */
    private var iOnBleItemClickLisenter = object : IBleItemClickLisenter {
        override fun onClick(bleDeviceList: ArrayList<BleRssiDevice>, connectType: Int) {

            context?.let {
                if (!Util.checkBleStatus(it)) return
                //单机连接  初始化最后一次连接信息
                if (connectType == EConnect.CONNECT_GROUP_TEST.data) {
                    BleUtil.devConnects(bleDeviceList)
                }
            }
        }
    }

    override fun onLayoutId(): Int {
        return R.layout.frag_third
    }

    override fun initView() {
        if(!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
//        return View.inflate(context, R.layout.frag_third, null)
    }

    override fun initListener() {
        rvChild.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
        rvChild.layoutManager = LinearLayoutManager(context)
        rvChild.adapter = bleGroupDialogFragAdapter

        bleGroupDialogFragAdapter.iOnBleItemClickLisenter = iOnBleItemClickLisenter

        //初始化刷新控件
        swipeRefreshLayout.setColorSchemeColors(Color.RED, Color.YELLOW, Color.GREEN)
        //刷新监听
        swipeRefreshLayout.setOnRefreshListener {
            activity?.let {
                if (!Util.checkBleStatus(activity!!)) {
                    swipeRefreshLayout.isRefreshing = false
                } else {
                    devDisconnectAll()
                    if (ble.isBleEnable) {
                        devScan()
                    } else {
                        swipeRefreshLayout.isRefreshing = false
                    }
                }
            }
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onBleEvent(event: BleEvent) {
        when (event.devStatus) {
            BleStatus.SCAN_PREPARE -> {
                BleUtil.bleRssiDeviceList.clear()
                bleGroupDialogFragAdapter.initData(groupData.groupKey.type)        //初始化蓝牙列表dapter
                if (!swipeRefreshLayout.isRefreshing) {
                    swipeRefreshLayout.isRefreshing = true
                }
            }

//            BleStatus.SCAN_RSSI_CHANGED -> {
//                val data = event.data
//                if (data != null) {
//                    bleGroupDialogFragAdapter.notifyItemChanged(data.toInt())
////                    bleGroupDialogFragAdapter.dataRefresh()
////                    bleGroupDialogFragAdapter.notifyDataSetChanged()
//                }
//            }

            BleStatus.SCAN_FOUND -> {
                bleGroupDialogFragAdapter.notifyDataSetChanged()
            }

            BleStatus.SCAN_STOP -> {
//                bleGroupDialogFragAdapter.dataRefresh()
                swipeRefreshLayout?.let {
                    if (swipeRefreshLayout.isRefreshing) {
                        swipeRefreshLayout.isRefreshing = false
                    }
                }
            }

            BleStatus.GATT_SERVICE_CONNECT -> {
//                val bleDevice = event.bleDevice
//                if (bleDevice != null) {
//                    if (!MotorTestDialogFrag.instance.isAdded) {
//                        MotorTestDialogFrag.show(activity!!)
//                    }
//                }
            }
        }
    }

    override fun onSelectDevice() {
        scanChildDevice()
    }

    override fun onSelectDeviceFinish() {
        val selectGroupValueList = bleGroupDialogFragAdapter.getSelectList()
        if (selectGroupValueList.size > 0) {
            groupData.groupValueList.clear()
            groupData.groupValueList.addAll(selectGroupValueList)
        }
    }

    private fun scanChildDevice() {
        ble.stopScan()
        devDisconnectAll()
        if (ble.isBleEnable) {
            devScan()
        } else {
            swipeRefreshLayout.isRefreshing = false
        }
    }


    override fun initDestroy() {
        EventBus.getDefault().unregister(this)
    }

}